<?php
namespace Tbf\Repository;
use Tbf\Util\FileSystemUtil;
/**
 * @deprecated
 */
class GitLocalMirror
{
    protected $origin_url;
    protected $dir_path;
    protected $process;
    public function __construct($di,$dir_path)
    {
        $this->process = $di['process'];
        $this->dir_path = $dir_path;
    }
    public function getDirPath()
    {
        return $this->dir_path;
    }
    /**
     * 必须满足下面条件:
     * 1.git命令存在
     * 2.当前目录是git的根目录
     * 3.当前git是一个裸git项目
     * */
    public function isExist()
    {
        try {
            $this->process->execute('git rev-parse --git-dir',$output,$this->getDirPath());
            $output = trim($output);
            if ($output!='.') {
                return false;
            }
            $this->process->execute('git config --get core.bare',$output,$this->getDirPath());
            $output = trim($output);
            if ($output!='true') {
                return false;
            }

            return true;
        } catch (\Exception $e) {
            //if git not exist ,the mirror should not exists too!
            return false;
        }
    }
    public function init()
    {
        if (!is_dir($this->getDirPath())) {
            FileSystemUtil::mkdir($this->getDirPath());
        }
        $this->process->execute('git init --bare',$output,$this->getDirPath());
    }
    public function cloneMirror($url)
    {
        FileSystemUtil::mkdir($this->getDirPath());
        $exec = sprintf('git clone --mirror %s %s',escapeshellarg($url),$this->getDirPath());
        $this->process->execute($exec,$output,$this->getDirPath());
        $this->origin_url = $url;
    }
    public function update()
    {
        $this->process->execute('git remote update --prune origin', $output, $this->getDirPath());
    }
    public function updateFrom($url)
    {
        $this->setOriginUrl($url);
        $this->update();
    }
    public function getOriginUrl()
    {
        $this->process->execute('git remote show -n origin',$output,$this->getDirPath());
        $ret = preg_match('/Fetch URL:(.*)/',$output,$matches);
        if (!$ret) {
            throw new \Exception(
                sprintf('get origin url fail! dir_path:%s output:%s',
                    $this->getDirPath(),
                    $output
                )
            );
        }
        $origin_url = $matches[1];
        $origin_url = trim($origin_url);

        return $origin_url;
    }
    public function setOriginUrl($url)
    {
        //it is ok if remote.origin no exists
        try {
            $this->process->execute('git remote rm origin',$output,$this->getDirPath());
        } catch (\Exception $e) {
        }
        $this->process->execute(sprintf('git remote add origin --mirror=fetch %s',escapeshellarg($url)),$output,$this->getDirPath());
    }
    public function getLastUpdateTime()
    {
        $fetch_head_path = $this->getDirPath().'/FETCH_HEAD';
        //it is not update by git fetch
        if (!file_exists($fetch_head_path)) {
            return 0;
        }
        //fetch fail?
        if (filesize($fetch_head_path)===0) {
            return 0;
        }
        $this->process->execute('stat -c %Y FETCH_HEAD',$output,$this->getDirPath());

        return trim($output);
    }
    public function gc()
    {
        $this->process->execute('git gc --prune=now',$output,$this->getDirPath());
    }
}
